#include "SCalendarWidget.h"
#include "Widgets/Input/SButton.h"
#include "Widgets/Layout/SConstraintCanvas.h"
#include "Layout/Visibility.h"
#include "Widgets/Text/STextBlock.h"
#include "TimeHelper.h"
#include "Widgets/Images/SImage.h"
#include "../MyStyle.h"
#include "../MySlateWidgetStyle.h"
//#include "../../../../Base/MyStyle.h"
//#include "../../../../Base/MySlateWidgetStyle.h"


void SCalendarWidget::Construct(const FArguments& InArgs)
{
	DateTime = InArgs._InDateTime;
	OndateChanged = InArgs._OndateChanged;
	AstrictDate = InArgs._InAstrictDate;
	if (DateTime)
	{
		InitButtons();
		FString WeekChar=TEXT("一二三四五六日");
		Year = TimeHelper::GetYear(*DateTime);
		Month = TimeHelper::GetMonth(*DateTime);
#pragma region 年控件
		TSharedRef<SHorizontalBox> YearWidget =
			SNew(SHorizontalBox)
			+ SHorizontalBox::Slot()
			.AutoWidth()
			.VAlign(EVerticalAlignment::VAlign_Center)
			[
				SNew(SButton)
				.OnClicked_Lambda([this]()
					{
						Year--;
						if(Year < 1980)
							Year = 1980;
						UpdataButtonsState();
						return FReply::Handled();
					})
				.Text(FText::FromString(TEXT("<<")))
			
			]
		
		+ SHorizontalBox::Slot()
			.FillWidth(1.f)
			.HAlign(EHorizontalAlignment::HAlign_Center)
			.VAlign(EVerticalAlignment::VAlign_Center)
			[
				SNew(STextBlock)//.TextStyle(&MyStyle::UIStyle->ALBBPUT_R_16_100)
				.Text_Lambda([this]() -> FText {
					return  FText::FromString(FString::FromInt(Year));
				})
			//.OnClicked(this, &SDateTimerWidget::LastYear_OnClicked)
			]
		
		+ SHorizontalBox::Slot()
			.AutoWidth()
			.VAlign(EVerticalAlignment::VAlign_Center)
			[
				SNew(SButton)
				.Text(FText::FromString(TEXT(">>")))
				.OnClicked_Lambda([this]()
					{
						Year++;
						UpdataButtonsState();
						return FReply::Handled();
					})
			];
#pragma endregion
		
#pragma region 月控件
		TSharedRef<SHorizontalBox> MonthWidget =
			SNew(SHorizontalBox)
			+ SHorizontalBox::Slot()
			.AutoWidth()
			.VAlign(EVerticalAlignment::VAlign_Center)
			[
				SNew(SButton)
				.Text(FText::FromString(TEXT("<<")))
				.OnClicked_Lambda([this]()
					{
						Month--;
						if(Month == 0)
						Month = 12;
						UpdataButtonsState();
						return FReply::Handled();
					})
			]
		
		+ SHorizontalBox::Slot()
			.FillWidth(1.f)
			.HAlign(EHorizontalAlignment::HAlign_Center)
			.VAlign(EVerticalAlignment::VAlign_Center)
			[
				SNew(STextBlock)//.TextStyle(&MyStyle::UIStyle->ALBBPUT_R_16_100)
				.Text_Lambda([this]() -> FText {
					return  FText::FromString(FString::FromInt(Month));
				})
			]
		
		+ SHorizontalBox::Slot()
			.AutoWidth()
			.VAlign(EVerticalAlignment::VAlign_Center)
			[
				SNew(SButton)
				.Text(FText::FromString(TEXT(">>")))
				.OnClicked_Lambda([this]()
					{
						Month++;
						if (Month > 12)
							Month = 1;
						UpdataButtonsState();
						return FReply::Handled();
					})
			];
#pragma endregion

#pragma region 日控件
		TSharedRef<SConstraintCanvas> dayWidget = SNew(SConstraintCanvas);
		for (int i = 0; i < WEEK_DAY; i++)
		{
			dayWidget->AddSlot()
			.Offset(FMargin(i * 32, 0, 30, 20))
			.Alignment(FVector2D(0.0f, 0.0f))
			[
				SNew(STextBlock)//.TextStyle(&MyStyle::UIStyle->ALBBPUT_R_16_100)
				.Justification(ETextJustify::Center)//对齐方式
				.Text(FText::FromString(WeekChar.Mid(i,1)))
			];
		}
		for(int i = 0; i < MONTH_WEEK;i++)
		{
			for (int j = 0; j < WEEK_DAY; j++)
			{
				dayWidget->AddSlot().Offset(FMargin( j * 32,(i +1) * 22, 30, 20))
				.Alignment(FVector2D(0.0f, 0.0f))
				[
					BtnDay[i][j].ToSharedRef()
				];

			}
		}
#pragma endregion

#pragma region 合并
		ChildSlot
			[
				SNew(SBorder)
				.BorderImage(FCoreStyle::Get().GetBrush("Menu.Background"))
				.Padding(FMargin(5))
				[
					SNew(SVerticalBox)
					+ SVerticalBox::Slot()
					.AutoHeight()
					[
						SNew(SBox)
						.WidthOverride(220.f)
						.HeightOverride(30.f)
						[
							YearWidget
						]
					]
		
				+ SVerticalBox::Slot()
					.AutoHeight()
					[
						SNew(SBox)
						.WidthOverride(220.f)
						.HeightOverride(30.f)
						[
							MonthWidget
						]
					]
				+ SVerticalBox::Slot()
					.AutoHeight()
					[
						SNew(SBox)
						.WidthOverride(220.f)
						[
							dayWidget
						]
					]
				]
			];
#pragma endregion
	}
	UpdataButtonsState();
	
}

FReply SCalendarWidget::ButtonClick()
{
	for (int i = 0; i < MONTH_WEEK; i++)
	{
		for (int j = 0; j < WEEK_DAY; j++)
		{
			if (BtnDay[i][j]->IsHovered())//鼠标在那个按钮上盘旋也就是在那个按钮上按下的按钮
			{
				*DateTime = TimeHelper::DateToTimestamp(Year, Month, FCString::Atoi(*TextBlocks[i][j]->GetText().ToString()));
				OndateChanged.ExecuteIfBound(Year, Month, FCString::Atoi(*TextBlocks[i][j]->GetText().ToString()));
				//if (OnUpdateData.IsBound())
				//{
				//	OnUpdateData.Execute(Year,Month, FCString::Atoi(*TextBlocks[i][j]->GetText().ToString()));
				//}
				break;
			}
		}
	}
	FSlateApplication::Get().DismissAllMenus();//关闭日历菜单
	return FReply::Handled();
}

//初始化所有的按钮
void SCalendarWidget::InitButtons()
{
	for(int i = 0; i < MONTH_WEEK;i++)
	{
		for (int j = 0; j < WEEK_DAY; j++)
		{
			TextBlocks[i][j] = 
			SNew(STextBlock)
			.Text(FText::FromString(TEXT("1")))
				.Justification(ETextJustify::Center);//对齐方式;
			BtnDay[i][j] = SNew(SButton).OnClicked(this, &SCalendarWidget::ButtonClick)
			[
				SNew(SImage).Image(&MyStyle::UIStyle->CalendarImage)
				//TextBlocks[i][j].ToSharedRef()
			];

		}
	}
}

void SCalendarWidget::UpdataButtonsState()
{
	uint16 MonthFirstDayWeek = TimeHelper::GetFirstDayWeekForMonth(Year, Month);//本月第一天是星期几
	uint16 MonthDays = TimeHelper::GetMonthDays(Year, Month);//本月共有多少天
	int index = 0;

	//先将所有的按钮隐藏
	for (index = 0; index < MONTH_WEEK * WEEK_DAY; index++)
	{
		BtnDay[index / WEEK_DAY][index % WEEK_DAY]->SetVisibility(EVisibility::Hidden);
		TextBlocks[index / WEEK_DAY][index % WEEK_DAY]->SetColorAndOpacity(FLinearColor(0.0f, 0.0f, 0.0f));
	}

	for (int i = 0; i < MonthDays; i++)//再将该月对应的日期显示出来
	{
		int item = i + (MonthFirstDayWeek - 1);
		BtnDay[item / WEEK_DAY][item % WEEK_DAY]->SetVisibility(EVisibility::Visible);
		TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetText(FText::FromString(FString::FromInt(i + 1)));
		//if (AstrictDate&&(DisMode == DisabledMode::BEFORE && TimeHelper::DateToTimestamp(Year, Month, i + 1) < TimeHelper::Get00_00_00ToTimestamp((*AstrictDate)) ||
		//	DisMode == DisabledMode::LAFTER && TimeHelper::DateToTimestamp(Year, Month, i + 1) > TimeHelper::Get00_00_00ToTimestamp((*AstrictDate))))
		//{//超过该时间段则不给予点击
		//	TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(false);
		//	BtnDay[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(false);
		//}
		//else if (TimeHelper::DateToTimestamp(Year, Month, i + 1) == TimeHelper::Get00_00_00ToTimestamp((*DateTime)))
		//{
		//	TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(true);
		//	BtnDay[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(true);
		//	TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetColorAndOpacity(FLinearColor(1.0f, 0.0f, 0.0f));
		//}
		//else
		{
			TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(true);
			BtnDay[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(true);
		}
	}
}